import {
    fetchPositions,
    addNewFloor,
    editFloor,
    deletePosition,
    fetchFloorsSelector,
    addNewArea,
    editArea
  } from "@/api/position"

  export default {
    namespaced: true,
      actions: {
        async fetchPositions(context, payload){
            let {marketId, type} = payload
            try {
                const { data } = await fetchPositions(marketId, type)
                return data
            }catch (error){
                console.log("store-modules-position-index.js-fetchPositions", error)
            }
        },

        async addNewFloor(context, payload) {
          let {marketId, name} = payload
          try {
              const { data } = await addNewFloor(marketId, name)
              return data
          }catch (error){
              console.log("store-modules-position-index.js-addNewFlow", error)
          }
        },

        async editFloor(context, payload) {
          let {posId, marketId, name} = payload
          try {
              const { data } = await editFloor(posId, marketId, name)
              return data
          }catch (error){
              console.log("store-modules-position-index.js-editFloor", error)
          }
        },

        async deletePosition(context, payload) {
          let {posId, marketId} = payload
          try {
              const { data } = await deletePosition(posId, marketId)
              return data
          }catch (error){
              console.log("store-modules-position-index.js-deletePosition", error)
          }
        },

        async fetchFloorsSelector(context, payload) {
          let {marketId} = payload
          try {
              const { data } = await fetchFloorsSelector(marketId)
              var items = []
              console.log("dt", data)
              for(var i =0; i<data.length;i++) {
                items[i] =
                  {
                    value: data[i].posId,
                    label: data[i].name
                  }
              }
              return items
          }catch (error){
              console.log("store-modules-position-index.js-deletePosition", error)
          }
        },

        async fetchPositionSelector(context, payload) {
          let {marketId} = payload
          try {
              const { data } = await fetchFloorsSelector(marketId)
              var items = []
              for(var i =0; i<data.length;i++) {
                var children = []
                for(var j = 0; j < data[i].children.length; j++){
                  children[j] = {
                    value: data[i].children[j].posId,
                    label: data[i].children[j].name,
                  }
                }
                items[i] =
                  {
                    value: data[i].posId,
                    label: data[i].name,
                    children: children,
                  }
              }
              return items
          }catch (error){
              console.log("store-modules-position-index.js-fetchPositionSelector", error)
          }
        },

        async addNewArea(context, payload) {
          let {marketId, parentId, name} = payload
          try {
              const { data } = await addNewArea(marketId, parentId, name)
              return data
          }catch (error){
              console.log("store-modules-position-index.js-addNewArea", error)
          }
        },

        async editArea(context, payload) {
          let {posId, marketId, name, parentId} = payload
          try {
              const { data } = await editArea(posId, marketId, parentId, name)
              return data
          }catch (error){
              console.log("store-modules-position-index.js-editArea", error)
          }
        },
      }
  }